﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PACKETPARSERMAX20.CORE
{
    public class DefinitionContext
    {

        public DefinitionContext(Packet pPacket, ReaderBase reader, WoWCore core, List<uint> pDefinedOpcodeList)
        {
            Packet = pPacket;
            Reader = reader;
            Core = core;
            DefinedOpcodeList = pDefinedOpcodeList;
        }

        public uint Opcode { get { return Packet.Opcode; } }
        public string OpcodeName { get { return Packet.OpcodeName; } }
        public int Direction { get { return Packet.Direction; } }
        public byte[] Data { get { return Packet.Data; } }
        public int ClientBuild { get { return Reader.ClientBuild; } }
        public string ClientBuildName { get { return Reader.ClientBuildName; } }
        public string AccountName { get { return Reader.AccountName; } }
        //public System.Text.StringBuilder Result { get { return Packet.Result; } }
        public string Result { get { return Packet.Result.ToString(); } set { Packet.Result = new StringBuilder(value); } }

        public DateTime CreatedDate { get { return Packet.CreatedDate; } }
        public ReaderBase Reader { get; private set; }
        public WoWCore Core { get; private set; }
        private Packet Packet { get; set; }
        public string Warning { get { return Packet.Warning; } set { Packet.Warning = value; } }
        public List<uint> DefinedOpcodeList { get; private set; }

        public DefinitionBase GetDefinition()
        {
            return GetDefinition(OpcodeName);
        }

        public DefinitionBase GetDefinition(uint opcode)
        {
            string tOpcodeName = Packet.GetOpcodeName(opcode, ClientBuild);
            DefinitionBase def = null;

            Type obj = Type.GetType(string.Format("PACKETPARSERMAX20.CORE.DEFINITIONS.{0}_DEF", tOpcodeName));
            if (obj != null)
            {
                def = (DefinitionBase)Activator.CreateInstance(obj);
                def.Initialize(Clone(opcode, tOpcodeName));
            }

            if (def == null)
            {
                def = new DefinitionDefault(Clone(opcode, tOpcodeName));
            }

            return def;
        }

        public DefinitionContext Clone(uint opcode, string pOpcodeName)
        {
            DefinitionContext t = new DefinitionContext(new Packet(Packet.Id, Packet.CreatedDate, Packet.Direction, opcode, Packet.Data, Packet.Size, ClientBuild), Reader, Core, DefinedOpcodeList);
            return t;
        }

        public DefinitionBase GetDefinition(string pOpcodeName)
        {
            DefinitionBase def = null;

            Type obj = Type.GetType(string.Format("PACKETPARSERMAX20.CORE.DEFINITIONS.{0}_DEF", pOpcodeName));
            if (obj != null)
            {
                def = (DefinitionBase)Activator.CreateInstance(obj);
                def.Initialize(this);
            }

            if (def == null)
            {
                def = new DefinitionDefault(this);
            }

            return def;
        }



    }
}
